package com.example.myapplication11_10;

import androidx.appcompat.app.AppCompatActivity;

import android.graphics.Color;
import android.os.Bundle;

import com.github.mikephil.charting.charts.CandleStickChart;
import com.github.mikephil.charting.components.XAxis;
import com.github.mikephil.charting.components.YAxis;
import com.github.mikephil.charting.data.CandleData;
import com.github.mikephil.charting.data.CandleDataSet;
import com.github.mikephil.charting.data.CandleEntry;

import java.util.ArrayList;

public class CandleActivity extends AppCompatActivity {
    private CandleStickChart chart;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_candle);

        chart = findViewById(R.id.chart);
        chart.getLegend().setEnabled(false);
        chart.getDescription().setEnabled(false);

        ArrayList<CandleEntry> list = new ArrayList<>();
        for (int i = 0; i < 22; i++) {
            float val = (float) (Math.random() * 40);
            float high = (float) (Math.random() * 9) + 8f;
            float low = (float) (Math.random() * 9) + 8f;
            float open = (float) (Math.random() * 6) + 1f;
            float close = (float) (Math.random() * 6) + 1f;
            boolean even = i % 2 == 0;
            list.add(new CandleEntry(
                    i, val + high,
                    val - low,
                    even ? val + open : val - open,
                    even ? val - close : val + close
            ));
        }

        XAxis xAxis = chart.getXAxis();
        xAxis.setPosition(XAxis.XAxisPosition.BOTTOM);
        xAxis.setDrawGridLines(false);

        YAxis leftAxis = chart.getAxisLeft();
        leftAxis.setLabelCount(7, false);
        leftAxis.setDrawGridLines(false);
        leftAxis.setDrawAxisLine(false);

        YAxis rightAxis = chart.getAxisLeft();
        rightAxis.setEnabled(false);

        CandleDataSet set1 = new CandleDataSet(list, "set");
        set1.setDrawIcons(false);
        set1.setShadowColor(Color.GRAY);
        set1.setShadowWidth(1.2f);
        set1.setDecreasingColor(Color.RED);
        set1.setIncreasingColor(Color.GREEN);

        CandleData data = new CandleData(set1);
        chart.setData(data);
        chart.invalidate();

    }
}